package com.tjbklx1.pattern.factory.demo2;

import java.util.HashMap;
import java.util.Map;



/**
 * 延迟加载的工厂类
 * 
 * @author tjbklx1
 * @date 2018-10-10
 */
public class ProductFactory {
	
	private static final Map<String, Product> productMap = new HashMap<String, Product>();

	public static synchronized Product createProduct(String type) throws Exception {
		Product product = null;
		// 如果Map中已经有这个对象
		if (productMap.containsKey(type)) {
			product = productMap.get(type);
		} else {
			if (type.equals("Product1")) {
				product = new ConcreateProduct1();
			} else if (type.equals("Product2")) {
				product = new ConcreateProduct2();
			}
			// 同时把对象放到缓存容器中
			productMap.put(type, product);
		}
		return product;
	}
}